﻿@inject IUserService _userService
@inject IPopupService PopupService
@inject IHttpService _httpService
@inject NavigationManager NavigationManager
@inject IPageModelCatche<UserSpaceViewModel> _pageModelCatche
@inject IPageModelCatche<List<ProjectInfoViewModel>> _projectPageModelCatche
@inject IPageModelCatche<ProjectViewModel> _indexPageModelCatche
@inject IPageModelCatche<List<ProjectPositionInfoViewModel>> _positionPageModelCatche

@implements IDisposable

<MDCard Class="project-info-card mb-4 pa-4">
    <MDCard Outline Class="user-card on-background-text" Href="@($"/user/space/{Model.CreateUser.Id}/1")">
         <CnGalWebSite.Components.Images.MasaImage Src="@Model.CreateUser.Avatar" Alt="@Model.CreateUser.Name" Type="ImageType.Avatar" />
         <div class="name-card">
             <div class="title-medium">@(Model.CreateUser.GetName(UserType.Organization))</div>
             <div class="label-large text-truncate-1">@(Model.CreateUser.OrganizationDescription)</div>
         </div>
     </MDCard>
     <div class="item-card">
         <div class="label-large">截止日期</div>
         <div class="title-large primary-text">@Model.EndTime.ToString("yyyy-MM-dd")</div>
     </div>
     @if (Model.Positions.Any())
    {
        @if (Model.Positions.Sum(s => s.BudgetMax) > 0)
        {
            <div class="item-card">
                <div class="label-large">总体预算区间</div>
                <div class="title-large primary-text">@($"￥ {Model.Positions.Sum(s => s.BudgetMin)} ~ {Model.Positions.Sum(s => s.BudgetMax)}")</div>
            </div>
        }
        @if (Model.Positions.Average(s => s.Percentage) > 0)
        {
            <div class="item-card">
                <div class="label-large">总体分成比例</div>
                <div class="title-large primary-text">@($"{Model.Positions.Average(s => s.Percentage)} %")</div>
            </div>
        }
        @if (Model.Positions.Sum(s => s.BudgetMax) == 0 && Model.Positions.Average(s => s.Percentage) == 0)
        {
            <div class="item-card">
                <div class="label-large">报酬</div>
                <div class="title-large primary-text">为爱发电</div>
            </div>
        }
    }
    else
    {
        <div class="item-card">
            <div class="label-large">约稿</div>
            <div class="title-large primary-text">暂无</div>
        </div>
    }
    @if (_userService.UserInfo.Id == Model.CreateUser.Id)
    {
        <div class="action-card">
            <CnGalWebSite.Components.Buttons.MasaButton Icon="@IconType.Edit.ToIconString()" Text="编辑企划" Rounded Href="@($"/project/edit/{Model.Id}")" />
            <CnGalWebSite.Components.Buttons.MasaButton TextStyle Icon="@IconType.Delete.ToIconString()" Text="删除" OnClick="OnDelete" />
         </div>
    }
</MDCard>

@code {
    [Parameter]
    public ProjectViewModel Model { get; set; }

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    protected override void OnInitialized()
    {
        _userService.UserInfoChanged -= OnUserInfoChanged;
        _userService.UserInfoChanged += OnUserInfoChanged;
    }

    public void OnUserInfoChanged()
    {
        StateHasChanged();
    }
    
    public async Task OnDelete()
    {
        var confirmed = await PopupService.ConfirmAsync("删除企划", "删除企划将会清空所有相关数据，确定要删除吗？", AlertTypes.Error);
        if (confirmed == false)
        {
            return;
        }

        string operationName = "删除企划";
        try
        {
            var obj = await _httpService.PostAsync<HideModel, Result>("api/project/Hide", new HideModel { Id = Model.Id, Hide = true });

            if (obj.Success == false)
            {
                await PopupService.ToastAsync(operationName + "失败", AlertTypes.Error);
            }
            else
            {
                await PopupService.ToastAsync(operationName + "成功", AlertTypes.Success);
                _projectPageModelCatche.Clean($"api/project/getall");
                _positionPageModelCatche.Clean($"api/project/getallpositions");
                _indexPageModelCatche.Clean($"api/project/get?id={Model.Id}");
                _pageModelCatche.Clean($"api/user/GetUserSpace?id={_userService.UserInfo.Id}");
                NavigationManager.NavigateTo("/project");
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, operationName + "失败");
        }

    }

    public void Dispose()
    {
        _userService.UserInfoChanged -= OnUserInfoChanged;
        GC.SuppressFinalize(this);
    }
}
